<!--  -->
<template>
  <div class="top-category">
  <div class="container">
    <!-- 面包屑 -->
    <el-breadcrumb :separator-icon="ArrowRight" class="first-class-bread">
      <el-breadcrumb-item class="first-class-bread-item" to="/">首页</el-breadcrumb-item>
      <transition name="fade-right" mode="out-in">
        <el-breadcrumb-item :key="firstClassList.id">{{firstClassList.name}}</el-breadcrumb-item>
      </transition>
    </el-breadcrumb>
    <!-- 轮播图 -->
    <HomeCarousel :bannerList="sliders" style="height:500px"/>
    <!-- 全部分类 -->
    <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in firstClassList.children" :key="item.id">
            <a href="javascript:;">
              <img :src="item.picture" >
              <p>{{item.name}}</p>
            </a>
          </li>
        </ul>
      </div>
    <!-- 各个分类推荐商品 -->
    <div class="ref-goods" v-for="item in firstClassList.children" :key="item.id">
      <div class="head">
        <h3>- {{item.name}} -</h3>
        <p class="tag">{{item.desc}}</p>
        <XtxMore />
      </div>
      <div class="body">
        <GoodsItem v-for="g in item.goods" :key="g.id" :goods="g" />
      </div>
    </div>
  </div>
  </div>
</template>

<script>
import { ArrowRight } from '@element-plus/icons'
import { useRoute } from 'vue-router'
import { findFirstClass } from '@/api/category.js'
import { ref, watch } from 'vue'
import { findBanner } from '@/api/home.js'
import HomeCarousel from '@/views/home/components/home-carousel.vue'
import GoodsItem from './components/goods-item'
export default {
  name : 'TopCategory',
  components: {
    HomeCarousel,
    GoodsItem
  },
  setup(){
    const route = useRoute()
    // 轮播图
    const sliders = ref([])
    const firstClassList = ref([])
    // 获取轮播图
    findBanner().then(data => {
      sliders.value = data.result
    })
    const getSubList = () => {
      findFirstClass(route.params.id).then(data => {
        firstClassList.value = data.result
      })
    }
    watch(() => route.params.id, (newVal) => {
      if (route.path === `/category/${newVal}`) {
        getSubList()
      }
    }, { immediate: true })
    return{
      ArrowRight,
      firstClassList,
      sliders
    }
  },
}
</script>
<style lang='less' scoped>
.top-category {
  :deep(.first-class-bread) {
    padding: 20px 0;
    .el-breadcrumb__inner.is-link {
        font-weight: unset !important;
      }
    .first-class-bread-item:hover {
      span {
        color: @xtxColor;
        font-weight: unset;
      }
    }
  }
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
    .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }

}
</style>